﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;


public partial class SendEMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            div1.Visible = true;
            div2.Visible = false;
            this.chkAllDatabaseContacts.Visible = false;
            this.txtTo.Text = Request.QueryString["TO"].ToString();            

            if (Request.QueryString["TYPE"].ToString() == "INSTANCE")
            {
                this.chkAllDatabaseContacts.Visible = true;
                this.txtSubject.Text = "DBA Instance EMail - " + Request.QueryString["Instance"].ToString();
                this.txtMessage.Text = 
                   "-----------------------------------------------------" + " \r\n" +
                   "Instance    : " + Request.QueryString["Instance"].ToString() + " \r\n" +
                   "-----------------------------------------------------" + " \r\n" +
                   "" + "\r\n" +
                   "-----------------------------------------------------" + " \r\n" +
                   "END OF MESSAGE" + " \r\n";
            }
            if (Request.QueryString["TYPE"].ToString() == "DATABASE")
            {
                this.txtSubject.Text = "DBA Database EMail - " + Request.QueryString["Database"].ToString();
                this.txtMessage.Text =
                   "-----------------------------------------------------" + " \r\n" +
                   "Instance    : " + Request.QueryString["Instance"].ToString() + " \r\n" +
                   "Database    : " + Request.QueryString["Database"].ToString() + " \r\n" +
                   "-----------------------------------------------------" + " \r\n" +
                   "" + "\r\n" +
                   "-----------------------------------------------------" + " \r\n" +
                   "END OF MESSAGE" + " \r\n";
            }
            if (Request.QueryString["TYPE"].ToString() == "DatabaseVerification")
            {
                this.txtSubject.Text = "Database Verification Request";
                this.txtMessage.Text =
                   "You are receiving this email because you are the designated contact for a database that has not been verified within the last 6 months." + " \r\n" +
                   "" + " \r\n" +
                   "Please click the following link to verify designated databases." + " \r\n" +
                   "" + " \r\n" +
                   "" + Session["sHTTP_PATH"].ToString()+"DatabaseVerification.aspx?PrimaryContactEMail="+Request.QueryString["TO"].ToString()+ " \r\n" +
                   "" + " \r\n" +
                   "To ensure accurate contact information databases require verification every 6 months." + " \r\n" +
                   "-----------------------------------------------------" + " \r\n" +
                   "END OF MESSAGE" + " \r\n";
            }
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage msg = new MailMessage();

            msg.To.Add(this.txtTo.Text.ToString());
            msg.Subject = this.txtSubject.Text.ToString();
            msg.Body = this.txtMessage.Text.ToString();

            SmtpClient client = new SmtpClient();
            client.Send(msg);

            MessageBox.Show("EMail sent successfully.");
            return;
        }
        catch (Exception err)
        {
            MessageBox.Show("There was a problem sending your EMail message. ERROR: " + err.Message.ToString());
            return;
        }

        
    }
    protected void chkAllDatabaseContacts_CheckedChanged(object sender, EventArgs e)
    {
        this.txtTo.Text =  Utility.DatabaseContactEMailByInstanceString(Request.QueryString["Instance"].ToString()).ToString();
    }
}
